
import Taro from '@tarojs/taro';
import { View, Image, Text } from '@tarojs/components';

import http from '@/utils/http';
import utils from '@/utils/util'

import './index.scss';
import DrugItem from '@/components/drugItem/index';
import MoreFooter from '@/components/moreFooter/index';
import StatusPages from '@/components/statusPages/index';

let app = Taro.getApp();
class DrugAdministration extends Taro.Component {
	constructor(props) {
		super(props);
		this.state = {
			drugList: [],
			erromessage: '网络错误，请点击屏幕重试',
			nearExpireTotal: 0,
			moreType: 'none',
			statusType: 'none', // none 不显示空白页  empty //显示空白页
			statuserro: false, // 是否显示错误页
		}
	}
	config = {
		navigationBarTitleText: '近效期药品',
		enablePullDownRefresh: true
	}
	tapDetails(e, item) {
		if (item.status === 3 && (item.isJxq === '将过期' || item.isJxq === '已过期')) {
			return utils.showtoast('该近效期药品已处于回收状态，无法操作')
		}
		//  else if (item.isJxq === '已过期'){
		// 	return utils.showtoast('该近效期药品已处于过期状态，无法操作')
		// }
		Object.keys(item).map((items) => {
			item[items] = encodeURIComponent(item[items])
		})
		Taro.navigateTo({
			url: '/pages/nearlyEffective/recyclingSubmit/index?' + utils.queryconcat(item)
		})
	}
	componentDidShow() {
		this.getJxqDrug();
	}
	onPullDownRefresh() {
		this.getJxqDrug();
	}
	getJxqDrug() {
		const userInfo = utils.getUserInfo();
		let { drugList, moreType } = this.state;
		let wardid = app.getValueSync('lastwardId');
		http.get('getJxqDrug', {
			wardid: wardid || userInfo && userInfo.ward
		}).then((res) => {
			Taro.stopPullDownRefresh();
			let MedicalkitArr = res.datas;
			if (res.datas instanceof Array === false) {
				MedicalkitArr = [res.datas];
			}
			let list = [];
			let nearExpireTotal = 0;
			MedicalkitArr.map((item) => {
				let rdata = JSON.parse(JSON.stringify(item));
				delete rdata['mark'];
				delete rdata['pics'];
				delete rdata['medicalkits'];
				delete rdata['drugs'];
				item.medicalkits.map((items) => {
					nearExpireTotal += (items.quantity - items.usernum);
					list.push({
						...rdata,
						...items,
						rmedicalkits: item.id,
						isJxq: +new Date(items.validitydate.replace(/-/g, '/')) < +new Date() ? '已过期' : '将过期'
					})
				})
			})
			moreType = "noMore";
			this.setState({
				moreType,
				nearExpireTotal,
				statusType: list.length > 0 ? 'none' : 'empty',
				statuserro: false,
				drugList: list
			});
		}).catch((e) => {
			console.error(e);
			Taro.stopPullDownRefresh();
			this.setState({
				erromessage: e && e.datas || '网络错误，请点击屏幕重试',
				statusType: drugList.length > 0 ? 'none' : 'empty',
				statuserro: drugList.length > 0 ? false : true,
			})
		})
	}
	refresh() {
		this.getJxqDrug();
	}
	render() {
		const { drugList, nearExpireTotal, statusType, statuserro, moreType, erromessage } = this.state;
		return (
			<View className='nearly-effective ov_h'>
				<View className='pl-3 pr-3 length-box ft28 flex flex-r flex-item-between flex-item-v-center mt-2-5'>
					<Text className='ft_w600 fc_53C ft32'>近效期药品<Text className='fc_960'>{nearExpireTotal}支</Text></Text>
				</View>
				<View className='pl-2-5 pr-2-5 mt-2-5'>
					{
						drugList.map((item) => {
							return (
								<DrugItem isnear drugItem={item} key={item.id} itemTap={(e, obj) => { this.tapDetails(e, obj) }}></DrugItem>
							)
						})
					}
					<MoreFooter moreType={moreType} restloading={() => { this.getJxqDrug() }} ></MoreFooter>
				</View>
				<StatusPages
  mt='mt-20'
  refresh={() => { this.refresh() }}
  statusType={statusType}
  statuserro={statuserro}
  erromessage={erromessage}
				></StatusPages>
			</View>
		);
	}
}

export default DrugAdministration;